package com.example.timyag.rest;


import java.util.List;

import com.example.timyag.dao.entity.Book;
import com.example.timyag.dao.mapper.BookMapper;
import com.example.timyag.service.ITestService;
import com.example.timyag.service.ITestServiceb;
import com.example.timyag.service.bo.BookBo;
import com.thot.timyag.core.HotDeployContext;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author yangjf
 * @time 2019-04-10 9:23
 */
@RestController
@RequestMapping("/api/v1/example")
public class TestController {

    @Autowired
    private HotDeployContext context;

    @Autowired
    private BookMapper bookMapper;

    @RequestMapping(value = "/uploadJar", method = RequestMethod.POST)
    @ApiOperation(value = "替换jar包", notes = "")
    @ResponseBody
    public String replaceLib(@ApiParam("上传jar包") @RequestParam("file") MultipartFile multipartFile) {
        // 添加自己逻辑支持拓展
        context.updateJarDependence(multipartFile);
        return "success";
    }

    @RequestMapping(value = "/testImpl", method = RequestMethod.GET)
    @ResponseBody
    public String testImpl(@ApiParam("名称") @RequestParam(value = "p1", required = false) String p1) {

        List<Book> books = bookMapper.selectAll();
        System.out.println("==========测试自身mapper===========");
        for (Book book : books) {
            System.out.println(book);
        }
        //测试外部实现
        System.out.println("===========测试外部基础实现=========");
        ITestService service = context.getBean(ITestService.class);
        String result = service.sayHello(p1);
        System.out.println(result);
        System.out.println("============测试外部mapper==========");
        BookBo bo = service.addBook(p1);
        System.out.println(bo);
        return result + "-" + bo;
    }

    @RequestMapping(value = "/testImpl2", method = RequestMethod.GET)
    @ResponseBody
    public String testImpl2(@ApiParam("名称") @RequestParam(value = "p1", required = false) String p1) {

        List<Book> books = bookMapper.selectAll();
        System.out.println("==========测试自身mapper===========");
        for (Book book : books) {
            System.out.println(book);
        }
        //测试外部实现
        System.out.println("===========测试外部基础实现=========");
        ITestServiceb serviceB = context.getBean(ITestServiceb.class);
        String result = serviceB.sayHello(p1);
        System.out.println(result);
        System.out.println("============测试外部mapper==========");
        BookBo bo = serviceB.addBook(p1);
        System.out.println(bo);
        return result + "-" + bo;
    }
}
